<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>将字符串转换为对象</title>
  </head>
  <body>
    <!-- 将字符串转换为对象 -->
    <script>
      var str = "name:'张三',age:18";
      var arr = [];
      var obj = {};
      arr = str.split(",");
      console.log(arr);
      for (var i in arr) {
        var newArr = [];
        newArr = arr[i].split(":");
        obj[newArr[0]] = newArr[1];
      }
      console.log(obj);
    </script>
  </body>
</html>
